//
//  mb_SoundSource.c
//  MBAudio
//
//  Created by avansc on 4/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <stdlib.h>
#include <stdio.h>

#include <OpenAL/al.h>

#include "MBAudio.h"

#ifdef __cplusplus
extern "C" {
#endif
    extern mb_pUIMap_t *mb_init_pUIMap();
    extern uint32_t mb_get_pUIMap(mb_pUIMap_t *h, const char *key);
    extern uint32_t mb_put_pUIMap(mb_pUIMap_t *h, const char *key, int *ret);
#ifdef __cplusplus
}
#endif

static mb_SoundSourceMap_t *g_SoundSourceMap = NULL;

static void initSoundSourceMap(void);
static int setSoundSourceMap(const char* key, unsigned int *val);
static unsigned int *getSoundSourceMap(const char* key);
static int addSoundSource(unsigned int buf);
static int removeSoundSource(unsigned int buf);

mb_SoundSourceMap_t *createSoundSourceMap(void)
{
    mb_SoundSourceMap_t *map = (mb_SoundSourceMap_t*)malloc(sizeof(mb_SoundSourceMap_t));
    
    map->m_initialized = 0;
    
    map->init = &initSoundSourceMap;
    map->set = &setSoundSourceMap;
    map->get = &getSoundSourceMap;
    map->add = &addSoundSource;
    map->remove = &removeSoundSource;
    
    if(!g_SoundSourceMap)
    {
        setActiveSoundSourceMap(map);
        initSoundSourceMap();
    }
    
    return map;
}

mb_SoundSourceMap_t *setActiveSoundSourceMap(mb_SoundSourceMap_t *map)
{
    mb_SoundSourceMap_t *temp = g_SoundSourceMap;
    g_SoundSourceMap = map;
    return temp;
}

void initSoundSourceMap(void)
{
    if (g_SoundSourceMap->m_initialized == 1)
        return;
    g_SoundSourceMap->m_initialized = 1;
    g_SoundSourceMap->m_sourcesMap = mb_init_pUIMap();
}

int setSoundSourceMap(const char* key, unsigned int *val)
{
    int ret;
    uint32_t k = mb_put_pUIMap(g_SoundSourceMap->m_sourcesMap, key, &ret);
    (g_SoundSourceMap->m_sourcesMap)->vals[k] = val;
    return k;
}

unsigned int *getSoundSourceMap(const char* key)
{
    return (g_SoundSourceMap->m_sourcesMap)->vals[mb_get_pUIMap(g_SoundSourceMap->m_sourcesMap, key)];
}

int addSoundSource(unsigned int buf)
{
    // TODO
    return 0;
}

int removeSoundSource(unsigned int buf)
{
    // TODO
    return 0;
}